﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Globalization;
using System.Threading;
namespace Utility
{
    public  class ConvertValueObject
    {
        #region Variable
        public static int PossibilityDoctorRecord = 10;
        private const short SHORT_MIN_VALUE = short.MinValue;
        private const int INT_MIN_VALUE = int.MinValue;
        private const long LONG_MIN_VALUE = long.MinValue;
        private const double DOUBLE_MIN_VALUE = double.MinValue;
        private const decimal DECIMAL_MIN_VALUE = decimal.MinValue;
        private static DateTime DATETIME_MIN_VALUE = DateTime.MinValue;
        private static char CHAR_MIN_VALUE = char.MinValue;
        private static string PARAM_SEPARATOR = ",";
        private static string PARAM_FORMAT_DATETIME = "dd/MM/yyyy";
        #endregion

        #region Methods
        public static object CastObjValueToInsertDB(object obj)
        {
            if (obj == null)
                return obj;
            string nameOfType = obj.GetType().Name;

            switch (nameOfType)
            {
                case "Int32":
                    {
                        int value = int.Parse(obj.ToString());
                        if (value == int.MinValue)
                            return null;
                        else
                            return value;

                    }
                case "Int64":
                    {
                        Int64 value = Int64.Parse(obj.ToString());
                        if (value == Int64.MinValue || value == int.MinValue)
                            return null;
                        else
                            return value;

                    }
                case "Decimal":
                    {
                        Decimal value = Decimal.Parse(obj.ToString());
                        if (value == Decimal.MinValue || value == Decimal.MinValue)
                            return null;
                        else
                            return value;

                    }

                case "Double":
                    {

                        double result;
                        result = Convert.ToDouble(obj);
                        if (result == Double.MinValue)
                        {
                            return null;
                        }
                        return result;
                    }

                case "DateTime":
                    {
                        DateTime value = DateTime.Parse(obj.ToString());
                        if (value == DateTime.MinValue)
                            return null;
                        else
                            return value;

                    }
                case "String":
                    {
                        if (obj.ToString().Equals(string.Empty))
                            return null;
                        else
                        {
                            string result = obj.ToString();
                           // result = result.Trim().Replace("'", "''");
                            return result;
                        }
                    }
                case "Boolean":
                    {
                        try
                        {
                            return bool.Parse(obj.ToString());
                        }
                        catch
                        {
                            return null;
                        }
                    }
                case "Guid":
                    {
                        Guid nullValue = Guid.Empty;
                        if (obj.ToString().Equals(nullValue.ToString()))
                            return null;
                        else
                        {
                            return obj;
                        }
                    }

                case "Byte[]":
                    {
                        return obj;
                    }
                case "Byte":
                    {
                        byte result;
                        try
                        {
                            result = Convert.ToByte(obj);
                        }
                        catch {
                            result = byte.MinValue;
                        }
                        if (result ==byte.MinValue)
                        {
                            return null;
                        }
                        return result;

                    }
            }

            return null;

        }
       
        public static object ParseBoolToDB(bool value)
        {
            try
            {
                return (object)value;
            }
            catch { return false; }
        }
        public static object ParseShortToDB(short value)
        {
            return (value == SHORT_MIN_VALUE) ? DBNull.Value : (object)value;
        }
        public static object ParseIntToDB(int value)
        {
            return (value == INT_MIN_VALUE) ? DBNull.Value : (object)value;
        }
        public static object ParseLongToDB(long value)
        {
            return (value == LONG_MIN_VALUE) ? DBNull.Value : (object)value;
        }
        public static object ParseDoubleToDB(double value)
        {
            return (value == DOUBLE_MIN_VALUE) ? DBNull.Value : (object)value;
        }
        public static object ParseDecimalToDB(decimal value)
        {
            return (value == DECIMAL_MIN_VALUE) ? DBNull.Value : (object)value;
        }
        public static object ParseDateTimeToDB(DateTime value)
        {
            return (value == DATETIME_MIN_VALUE) ? DBNull.Value : (object)value;
        }
        public static object ParseStringToDB(string value)
        {
            return (value == null || value == string.Empty) ? DBNull.Value : (object)value;
        }
        public static object ParseCharToDB(char value)
        {
            return (value == CHAR_MIN_VALUE) ? DBNull.Value : (object)value;
        }
        //
        public static bool ParseDBToBool(IDataReader dre, string column)
        {
            return dre.IsDBNull(dre.GetOrdinal(column)) ? false : (bool)(dre[column]);
        }

        public static int ParseDBToInt(IDataReader dre, string column)
        {
            int index = dre.GetOrdinal(column);
            bool has = dre.IsDBNull(dre.GetOrdinal(column));
            string val = string.Empty;
            if (has == false)
            {
                val = dre.GetInt32(index).ToString();
            }
            return has ? INT_MIN_VALUE : int.Parse(val);
        }
        public static short ParseDBToShort(IDataReader dre, string column)
        {
            return dre.IsDBNull(dre.GetOrdinal(column)) ? Int16.MinValue : Int16.Parse(dre[column].ToString());
        }

        public static int ParseDBToSmallInt(IDataReader dre, string column)
        {
            int index = dre.GetOrdinal(column);
            bool has = dre.IsDBNull(dre.GetOrdinal(column));
            string val = string.Empty;
            if (has == false)
            {
                val = dre.GetInt16(index).ToString();
            }
            return has ? INT_MIN_VALUE : int.Parse(val);
        }

        public static short ParseDBToTinyInt(IDataReader dre, string column)
        {
            int index = dre.GetOrdinal(column);
            bool has = dre.IsDBNull(dre.GetOrdinal(column));
            string val = string.Empty;
            if (has == false)
            {
                val = dre.GetByte(index).ToString();
            }
            return has ? SHORT_MIN_VALUE : short.Parse(val);
        }

        public static long ParseDBToBigInt(IDataReader dre, string column)
        {
            int index = dre.GetOrdinal(column);
            bool has = dre.IsDBNull(index);
            return has ?
              LONG_MIN_VALUE : dre.GetInt64(dre.GetOrdinal(column));
        }
        public static long ParseDBToLong(IDataReader dre, string column)
        {
            int index = dre.GetOrdinal(column);
            bool has = dre.IsDBNull(index);
            return has ?
              LONG_MIN_VALUE : dre.GetInt64(dre.GetOrdinal(column));
        }
        public static double ParseDBToDouble(IDataReader dre, string column)
        {
            return dre.IsDBNull(dre.GetOrdinal(column)) ?
              DOUBLE_MIN_VALUE : dre.GetDouble(dre.GetOrdinal(column));
        }
        public static decimal ParseDBToDecimal(IDataReader dre, string column)
        {
            return dre.IsDBNull(dre.GetOrdinal(column)) ?
              DECIMAL_MIN_VALUE : dre.GetDecimal(dre.GetOrdinal(column));
        }
        public static DateTime ParseDBToDateTime(IDataReader dre, string column)
        {
            return dre.IsDBNull(dre.GetOrdinal(column)) ?
              DATETIME_MIN_VALUE : dre.GetDateTime(dre.GetOrdinal(column));
        }
        public static string ParseDBToString(IDataReader dre, string column)
        {
            return dre.IsDBNull(dre.GetOrdinal(column)) ?
              string.Empty : dre.GetString(dre.GetOrdinal(column));
        }
        public static string ParseDBToGUI(IDataReader dre, string column)
        {
            int index = dre.GetOrdinal(column);
            bool has = dre.IsDBNull(index);
            return has ?
            string.Empty : dre.GetGuid(index).ToString();
        }

        public static char ParseDBToChar(IDataReader dre, string column)
        {
            return dre.IsDBNull(dre.GetOrdinal(column)) ?
            CHAR_MIN_VALUE : char.Parse(dre.GetString(dre.GetOrdinal(column)));
        }
        public static byte[] ParseDBToBinary(IDataReader dre, string column)
        {
            if (!dre.IsDBNull(dre.GetOrdinal(column)))
                return (byte[])dre[column];
            return new byte[] { };
        }
        public static object ParseDBToObject(IDataReader dre, string column)
        {
            if (!dre.IsDBNull(dre.GetOrdinal(column)))
                return (object)dre[column];
            return null;
        }
        //
        public static string ParseString(short value)
        {
            return (value == SHORT_MIN_VALUE) ? string.Empty : value.ToString();
        }
        public static string ParseString(int value)
        {
            return (value == INT_MIN_VALUE) ? string.Empty : value.ToString();
        }
        public static string ParseString(long value)
        {
            return (value == LONG_MIN_VALUE) ? string.Empty : value.ToString();
        }
        public static string ParseString(double _value)
        {
            if (_value < 0)
            {
                return string.Empty;
            }
            NumberFormatInfo nfi = (NumberFormatInfo)Thread.CurrentThread.CurrentCulture.NumberFormat.Clone();
            nfi.NumberGroupSeparator = ".";
            nfi.NumberDecimalSeparator = ",";
            nfi.NumberDecimalDigits = 2;

            nfi.CurrencyGroupSeparator = ".";
            nfi.CurrencyDecimalDigits = 2;
            nfi.CurrencyDecimalSeparator = ",";

            string result = _value.ToString("N", nfi);
            int index = result.IndexOf(nfi.NumberDecimalSeparator);
            if (index > 0)
            {
                string sdecimalValue = result.Substring(index + 1, nfi.NumberDecimalDigits);
                int idecimalValue = int.Parse(sdecimalValue);
                if (idecimalValue == 0)
                    result = result.Substring(0, index);
                return result;
            }
            else
                return result;
        }
        public static string ParseString(decimal value)
        {
            return (value == DECIMAL_MIN_VALUE) ? string.Empty : value.ToString();
        }
        public static string ParseString(DateTime value)
        {
            return (value == DATETIME_MIN_VALUE) ? string.Empty : value.ToString(PARAM_FORMAT_DATETIME);
        }
        public static string ParseString(DateTime value, string dateformat)
        {
            return (value == DATETIME_MIN_VALUE) ? string.Empty : value.ToString(dateformat);
        }
        public static string ParseString(char value)
        {
            return (value == CHAR_MIN_VALUE) ? string.Empty : value.ToString();
        }
        //
        public static short ParseShort(string value)
        {
            short result;
            value = value.Replace(PARAM_SEPARATOR, "").Trim();
            try { result = short.Parse(value); }
            catch { result = SHORT_MIN_VALUE; }
            return result;
        }
        public static int ParseInt(string value)
        {
            int result;
            value = value.Replace(PARAM_SEPARATOR, "").Trim();
            try { result = int.Parse(value); }
            catch { result = INT_MIN_VALUE; }
            return result;
        }
        public static byte ParseByte(string value)
        {
            byte result;
            value = value.Replace(PARAM_SEPARATOR, "").Trim();
            try { result =byte.Parse(value); }
            catch { result = byte.MinValue; }
            return result;
        }
        public static long ParseLong(string value)
        {
            long result;
            value = value.Replace(PARAM_SEPARATOR, "").Trim();
            try { result = long.Parse(value); }
            catch { result = LONG_MIN_VALUE; }
            return result;
        }
        public static double ParseDouble(string value)
        {
            double result;
            value = value.Replace(PARAM_SEPARATOR, "").Trim();
            try { result = double.Parse(value); }
            catch { result = DOUBLE_MIN_VALUE; }
            return result;
        }
        public static double ParseDoubleDiv100(string value)
        {
            double result;
            value = value.Replace(PARAM_SEPARATOR, "").Trim();
            try
            {
                result = double.Parse(value);
                result /= 100;
            }
            catch { result = DOUBLE_MIN_VALUE; }
            return result;
        }
        public static decimal ParseDecimal(string value)
        {
            decimal result;
            value = value.Replace(PARAM_SEPARATOR, "").Trim();
            try { result = decimal.Parse(value); }
            catch { result = DECIMAL_MIN_VALUE; }
            return result;
        }
        /*
        public static DateTime ParseDateTime(string value)
        { 
                string year;
                string month;
                string  day;
    
                if( value.Length != 8 && value.Length != 12 ) return DateTime.MinValue;
                if( value.IndexOf("/") >= 0) return DateTime.MinValue;
                year = value.Substring(0,4);
                month = value.Substring(4,2);
                day = value.Substring(6,2);  
			
                value = year + "/" + month + "/" + day;

          string hour = string.Empty;
          string minute = string.Empty;
          if( value.Length == 12 ){
            hour = value.Substring(8,2); 
            minute = value.Substring(10,2); 
            value += " "+ hour + ":" + minute + ":" + "00";
          }
          DateTime result;

                try { result =  DateTime.Parse(value); }
          catch { result = DATETIME_MIN_VALUE; }
          return result; 
        }
        */
        public static DateTime ParseDateTime(string value)
        {
            DateTime result;
            DateTimeFormatInfo info;
            info = (DateTimeFormatInfo)CultureInfo.CurrentUICulture.DateTimeFormat.Clone();
            info.ShortDatePattern = BaseFunctions.DEFAULT_DATE_FORMAT;
            info.FullDateTimePattern = BaseFunctions.DEFAULT_DATE_FORMAT;
            try { result = DateTime.Parse(value, info); }
            catch { result = DATETIME_MIN_VALUE; }
            return result; 
        }
        public static long ParseLongMul1000(string value)
        {
            long result;
            value = value.Replace(PARAM_SEPARATOR, "").Trim();
            try
            {
                result = long.Parse(value);
                result *= 1000;
            }
            catch { result = LONG_MIN_VALUE; }
            return result;
        }
        public static int GetValueInt32Reader(IDataReader reader,string column)
        {
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(column)))
                    return int.Parse(reader[column].ToString());
            }
            catch
            { 
            }
            return int.MinValue;
        }
        public static string GetValueStringReader(IDataReader reader, string column)
        {
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(column)))
                    return reader[column].ToString();
            }
            catch
            {
            }
            return string.Empty;
        }
        #endregion 
    }
}
